<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>票务管理-韩晓</title>
  
   <link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/themes/color.css">
    <link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/demo/demo.css">
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
</head>
<body>
    <h2>车票信息</h2>
   <!-- TicketControllerhx/list -->
    <table id="dg" title="车票信息" class="easyui-datagrid" style="width:700px;height:250px"
            url="../TicketControllerhx/list" 
            toolbar="#toolbar" pagination="true"
            rownumbers="true" fitColumns="true" singleSelect="true">
        <thead>
            <tr>
                <th field="tickedid" width="50">车票ID</th>
                <th field="trainno" width="50">车次</th>
                <th field="origin" width="50">出发站</th>
                <th field="destination" width="50">到达站</th>
                <th field="price" width="50">票价</th>
                <th field="ticketnum" width="50">数量</th>
            </tr>
        </thead>
    </table>
     
        
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">新增车票</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">修改车票</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除车票</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-load" plain="true" onclick="get()">更新</a>
    </div>
    
      <div id="dlg" class="easyui-dialog" style="width:400px"
            closed="true" buttons="#dlg-buttons">
        <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
            <div style="margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc">新增车票信息</div>
            <div style="margin-bottom:10px">
                <input  name="tickedid" class="easyui-textbox" required="true" label="车票ID:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="trainno" class="easyui-textbox" required="true" label="车次:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="origin" class="easyui-textbox" required="true" label="出发站:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="destination" class="easyui-textbox" required="true" validType="destination" label="终点站:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="price" class="easyui-textbox" required="true" validType="price" label="价格:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="ticketnum" class="easyui-textbox" required="true" validType="ticketnum" label="数量:" style="width:100%">
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">Save</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
    </div>
    <script type="text/javascript">
    
    
        var url;      
          function newUser(){
            $('#dlg').dialog('open').dialog('center').dialog('setTitle','添加票务信息');
            $('#fm').form('clear');
            url = '../TicketControllerhx/add';
        }
        function editUser(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $('#dlg').dialog('open').dialog('center').dialog('setTitle','修改票务信息');
                $('#fm').form('load',row);
                url = '../TicketControllerhx/update?id='+row.tickedid;
                $('#dg').datagrid('reload',{});    // reload the user data
            }
        }
        function saveUser(){
           $('#fm').form('submit',
           		{
               		url: url,
               		onSubmit: function(){return $(this).form('validate');},
	                success: function(result)
	                
	                
	                {
	                	
	                	
	                    var result = eval('('+result+')');
	                    if (result.errorMsg){
	                        $.messager.show({title: 'Error',msg: result.errorMsg});
	                    } else {
	                        $('#dlg').dialog('close');        // close the dialog
	                        $('#dg').datagrid('reload',{});    // reload the user data
	                    }
	                }
           		}
           );
        }
        function destroyUser(){
            var row = $('#dg').datagrid('getSelected');
            var tickedid;
            if (row){
                $.messager.confirm('Confirm','你确定要删除这条数据吗?',function(r){
                    if (r){
                        $.post('../TicketControllerhx/delete',{id:row.tickedid},function(result){
                            if (result.success){
                                $('#dg').datagrid('reload');    // reload the user data
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: result.errorMsg
                                });
                            }
                        },'json');
                    }
                });
            }
        }
        
        function createXMLHTTPRequest() {

        	// 1.创建XMLHttpRequest对象
        	// 这是XMLHttpReuquest对象无部使用中最复杂的一步
        	// 需要针对IE和其他类型的浏览器建立这个对象的不同方式写不同的代码
        	var xmlHttpRequest;
        	if (window.XMLHttpRequest) {
        		// 针对FireFox，Mozillar，Opera，Safari，IE7，IE8
        		xmlHttpRequest = new XMLHttpRequest();
        		// 针对某些特定版本的mozillar浏览器的BUG进行修正
        		if (xmlHttpRequest.overrideMimeType) {
        			xmlHttpRequest.overrideMimeType("text/xml");
        		}
        	} else if (window.ActiveXObject) {
        		// 针对IE6，IE5.5，IE5
        		// 两个可以用于创建XMLHTTPRequest对象的控件名称，保存在一个js的数组中
        		// 排在前面的版本较新
        		var activexName = [ "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ];
        		for (var i = 0; i < activexName.length; i++) {
        			try {
        				// 取出一个控件名进行创建，如果创建成功就终止循环
        				// 如果创建失败，回抛出异常，然后可以继续循环，继续尝试创建
        				xmlHttpRequest = new ActiveXObject(activexName[i]);
        				if (xmlHttpRequest) {
        					break;
        				}
        			} catch (e) {
        			}
        		}
        	}
        	
        	return xmlHttpRequest;
        }

        function get() {
        	var req = createXMLHTTPRequest();
        	if (req) {
        		req.open("GET", "../TicketController/list", true);
        		req.onreadystatechange = function() {
        			if (req.readyState == 4) {
        				if (req.status == 200) {
        					var ticket = JSON.parse(req.responseText);
        					//var msg = "person name=" + person.name + ";age=" + person.age + ";address=" + person.address;
        					document.getElementById("dg").innerHTML= ticket;
        				} else {
        					alert("error");
        				}
        			}
        		}
        		req.send(null);
        	}
        }

        function post() {
        	var req = createXMLHTTPRequest();
        	if (req) {
        		req.open("POST", "../TicketController/list", true);
        		req.setRequestHeader("Content-Type",
        				"application/x-www-form-urlencoded; charset=gbk;");
        		req.send("keywords=手机");
        		req.onreadystatechange = function() {
        			if (req.readyState == 4) {
        				if (req.status == 200) {
        					alert("success");
        				} else {
        					alert("error");
        				}
        			}
        		}
        	}
        }
        
        
        
    </script>
</body>
</html>